package com.spzx.common.rabbit.service;

import com.alibaba.fastjson2.JSON;
import com.spzx.common.core.utils.uuid.UUID;
import com.spzx.common.rabbit.entity.GuiguCorrelationData;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

//@Component
@Service
public class RabbitService {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 发送消息
     *
     * @param exchange   交换机
     * @param routingKey 路由键
     * @param message    消息
     */
   /* public boolean sendMessage(String exchange,String routingKey,Object message){
        rabbitTemplate.convertAndSend(exchange,routingKey,message);
        return true;
    }*/

    //发消息时携带关联数据，便于以后重发消息。
    public boolean sendMessage(String exchange, String routingKey, Object message) {

        //关联数据
        GuiguCorrelationData correlationData = new GuiguCorrelationData();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        correlationData.setId(uuid);
        correlationData.setExchange(exchange);
        correlationData.setRoutingKey(routingKey);
        correlationData.setMessage(message);

        //将关联数据保存到redis中，用于后续重试(主要给回退函数使用。)
        //先存redis再发消息，否则，先发消息可能会先执行回退处理，从缓存取不到关联数据。空指针。
        redisTemplate.opsForValue().set(uuid, JSON.toJSONString(correlationData), 10, TimeUnit.MINUTES);

        //发送消息
        rabbitTemplate.convertAndSend(exchange, routingKey, message, correlationData);

        return true;
    }


    /**
     * 利用延迟插件发送延迟方法
     * @param exchange
     * @param routingKey
     * @param message
     * @param delayTime
     * @return
     */
    public boolean sendDelayMessage(String exchange, String routingKey, String message, int delayTime) {

        //关联数据
        GuiguCorrelationData correlationData = new GuiguCorrelationData();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        correlationData.setId(uuid);
        correlationData.setExchange(exchange);
        correlationData.setRoutingKey(routingKey);
        correlationData.setMessage(message);

        correlationData.setDelay(true); // delay=true 表示延迟消息
        correlationData.setDelayTime(delayTime); //延迟时间秒

        //将关联数据保存到redis中，用于后续重试(主要给回退函数使用。)
        //先存redis再发消息，否则，先发消息可能会先执行回退处理，从缓存取不到关联数据。空指针。
        redisTemplate.opsForValue().set(uuid, JSON.toJSONString(correlationData), 10, TimeUnit.MINUTES);

        //发送消息
      /* MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(delayTime*1000); //单位毫秒。
                return message;
            }
        };*/
    //    rabbitTemplate.convertAndSend(exchange,routingKey,message,messagePostProcessor,correlationData);

        rabbitTemplate.convertAndSend(exchange,routingKey,message,(message1 -> {
            message1.getMessageProperties().setDelay(delayTime*1000); //单位毫秒。
            return message1;
        }),correlationData);


        return true;
    }


}
